package com.i360day.invoker.registry;

import org.springframework.context.EnvironmentAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.env.Environment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;

import java.util.function.Supplier;


/**
 * <p> @description:     <p>
 * <p>
 * <p> @author: 胡.青牛   <p>
 * <p>
 * <p> @date: 2019/5/11 0011  16:04
 **/
class RemoteAnnotationParserSupport implements ResourceLoaderAware, EnvironmentAware {

    protected ResourceLoader resourceLoader = new DefaultResourceLoader();
    protected Environment environment;
    private BeanDefinitionRegistrarFactory registrarFactory;

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    public ResourceLoader getResourceLoader() {
        return resourceLoader;
    }

    public Environment getEnvironment() {
        return environment;
    }

    public void setRegistrarFactory(BeanDefinitionRegistrarFactory registrarFactory) {
        this.registrarFactory = registrarFactory;
    }

    /**
     * 获取remoteScan下的class类注册
     *
     * @return
     */
    public BeanDefinitionRegistrarFactory getRegistrarFactory(Supplier<BeanDefinitionRegistrarFactory> supplier) {
        if (registrarFactory == null) {
            return this.registrarFactory = supplier != null ? supplier.get() : new RemoteScanImportBeanDefinitionRegistrar(resourceLoader, environment);
        }
        return this.registrarFactory;
    }

    /**
     * 获取remoteScan下的class类注册
     *
     * @return
     */
    public BeanDefinitionRegistrarFactory getRegistrarFactory() {
        return getRegistrarFactory(null);
    }
}
